package com.yy.base.util;

import com.mes.entity.BaseEntity;
import javax.persistence.Column;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;

@SuppressWarnings("unchecked")
public class ObjectUtil {
	private static SimpleDateFormat dateSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	public synchronized static String dateToString(Date date) {
		return dateSDF.format(date);
	}

	public synchronized static List<Map> listEntityToMap(List list) {
		List<Field> fieldes = new ArrayList<Field>();
		List<Map> result = new ArrayList();
		String name = null;
		String methodName = null;
		Object value = null;
		Map map = null;
		
		for (Object obj : list) {
			getFields(obj.getClass(), obj, fieldes);
			map = new HashMap();
			
			for (Field f : fieldes) {
				try {
					name = f.getName();
					methodName = "get" + name.substring(0, 1).toUpperCase() + name.substring(1, name.length());
					value = obj.getClass().getMethod(methodName).invoke(obj);
					if (value != null) {
						if (value instanceof Date) {
							map.put(name, dateSDF.format((Date)value));
						} else {
							map.put(name, value);
						}
					}
				} catch (Exception e) {
				}
			}
			result.add(map);
		}
		return result;
	}
	/**
	 * Map集合列表转换为实体列表
	 * @param <T>
	 * @param clzz
	 * @param list
	 * @return
	 */
	public synchronized static <T> List<T> listMapToEntity(Class<T> clzz, List<Map<String, Object>> list) {
		List<T> result = new ArrayList<T>();
		for (Map map : list) {
			result.add((T)mapToEntity(clzz, map));
		}
		return result;
	}
	/**
	 * 将值赋值实体类
	 */
	public synchronized static <T> T mapToEntity(Class<T> clzz, Map<String, Object> map) {
		if (map == null) {
			return null;
		}
		try {
			T t = clzz.newInstance();
			List<Field> fieldes = new ArrayList<Field>();
			getFields(clzz, t, fieldes);
			
			String fieldName = null;
			Object value = null;
			Method method = null;
			String annotName = null;

			for (Field field : fieldes) {
				fieldName = field.getName();
				try {
					if (field.getType().getName().indexOf("oolean") > -1) {
						if (fieldName.startsWith("is")) {
							method = clzz.getMethod(fieldName);
						} else {
							method = clzz.getMethod("is" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1));
						}
					} else {
						method = clzz.getMethod("get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1));
					}
					value = map.get(fieldName);

					if (value == null) {
						for (String key : map.keySet()) {
							if (method.isAnnotationPresent(Column.class)) {
								annotName = method.getAnnotation(Column.class).name();
								if (annotName != null && (
										annotName.toUpperCase().equals(key.replace("_", "").toUpperCase()) ||
										annotName.toUpperCase().equals(key.toUpperCase()))) {
									value = map.get(key);
									break;
								}
							}
							if (fieldName.toUpperCase().equals(key.replace("_", "").toUpperCase())) {
								value = map.get(key);
								break;
							}
						}
					}


					if (value != null) {
						if (value != null && !value.getClass().equals(field.getType())) {
							if (field.getType().getName().indexOf("oolean") > -1) {
								value = "Y".equals(value.toString());
							} else if (field.getType().getName().indexOf("Long") > -1) {
								value = Long.valueOf(value.toString());
							} else if (field.getType().getName().indexOf("Clob") > -1) {
								value = StringUtil.clobToString(value);
							} else if (field.getType().getName().indexOf("Double") > -1) {
								value = Double.valueOf(value.toString());
							} else if (field.getType().getName().indexOf("Float") > -1) {
								value = Float.valueOf(value.toString());
							} else if (field.getType().getName().indexOf("Integer") > -1) {
								value = Integer.valueOf(value.toString());
							} else if (field.getType().getName().indexOf("String") > -1) {
								value = value.toString();
							}
						}
						clzz.getMethod("set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1), field.getType()).invoke(t, value);
					}
				} catch (Exception e) {
					//System.out.println("查询数据注入实体对象错误：" + fieldName + "-" + field.getType() + "-" + value.getClass());
				}
			}
			return t;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}
	/**
	 * 获取需要转化的属性(包括父类的属性 )
	 */
	private static void getFields(Class clzz, Object obj, List<Field> list) {
		Field []fields = clzz.getDeclaredFields();
		for (Field f : fields) {
			list.add(f);
		}
		fields = clzz.getFields();
		for (Field f : fields) {
			list.add(f);
		}
		if (obj instanceof BaseEntity && !clzz.equals(BaseEntity.class)) {
			getFields(clzz.getSuperclass(), obj, list);
		}
	}
}